#include "player.h"
/*
void gotoxy(int x, int y)
{
    HANDLE hConsoleOutput;
    COORD dwCursorPosition;
    dwCursorPosition.X = x;
    dwCursorPosition.Y = y;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}*/

    /********************************/
    /*          Inheritance         */
    /********************************/
    int player::getObjectType() const {return tipe_object;}
    /********************************/
    /*      CTOR, CCTOR, DTOR       */
    /********************************/
    player::player():Object(1),MAX_AP(50) {AP = MAX_AP;}
    player::player (const player&):Object(1),MAX_AP(75) {}
    player::~player() {}

    /*********************************/
    /*            METHODS            */
    /*********************************/
    //=========  change vector ============
    void player::turn_left()
    {
        if (AP > 0)
            {vektor = location.left; face = 0; AP--;}
        else sleep();
    }
    void player::turn_right()   
    {
        if (AP > 0)
            {vektor = location.right; face = 1; AP--;}
        else sleep();
    }
    void player::turn_up()
    {
        if (AP > 0)
            {vektor = location.up; face = 2; AP--;}
        else sleep();
    }
    void player::turn_down()
    {
        if (AP > 0)
            {vektor = location.down; face = 3; AP--;}
        else sleep();
    }


    //=============  getter  ==============
    int player::getAP()         {return AP;}
    point player::getpos()      {return location.mid;}
    point player::getvektor() const {return vektor;}
    int player::getMoney()      {return money;}
    string player::getName()    {return name;}
    int player::getArea()	{return area;}
    int player::getFace()	{return face;}
    int player::getMAXAP() const {return MAX_AP;}
    bool player::isInvEmpty(int nslot) const
    {
        switch(nslot)
        {
            case 1: return inventory1.empty(); break;
            case 2: return inventory2.empty(); break;
            case 3: return inventory3.empty(); break;
        }
    }
    //getInvAt perilakunya tidak terdefinisi jika inventory tersebut kosong !!! cek dulu isInvEmpty baru pake method ini
    Object* player::getInvAt(int nslot) const
    {
        switch(nslot)
        {
            case 1: if(!isInvEmpty(nslot)) return inventory1.back(); break;
            case 2: if(!isInvEmpty(nslot)) return inventory2.back(); break;
            case 3: if(!isInvEmpty(nslot)) return inventory3.back(); break;
        }
    }
    int player::getInvSize(int nslot) const
    {
        switch(nslot)
        {
            case 1: return inventory1.size(); break;
            case 2: return inventory2.size(); break;
            case 3: return inventory3.size(); break;
        }
    }

    //=============  setter  ==============
    void player::setAP(int action)  {AP = action;}
    void player::setpos(int x, int y)
    //set player position
    {   // Old ones
        location.mid.x   = x;   location.mid.y   = y;
        location.left.x  = x-1; location.left.y  = y;
        location.right.x = x+1; location.right.y = y;
        location.up.x    = x;   location.up.y    = y-1;
        location.down.x  = x;   location.down.y  = y+1;
    }
    void player::setvektor(point value)	{vektor.x = value.x;vektor.y = value.y;}
    void player::setName(string Name)   {name = Name;}
    void player::setMoney(int Money)    {money = Money;}
    void player::setArea(int value)	{area = value;}
    void player::setInventory(int nslot,int ID,int quantity)	//masukin ke inventory (OTOMATIS ERASE, BARU INSERT ITEM)
    {
        Object* o;
        switch(ID)
        {
            case 51: o = new Bibit(51,10,5); break; //cabai
            case 52: o = new Bibit(52,5,2); break; //wortel
            case 53: o = new Bibit(53,8,4); break; //jamur
            case 54: o = new Bibit(54,6,3); break; //melon
            case 55: o = new Bibit(55,7,3); break; //jagung
            case 56: o = new Bibit(56,6,3); break; //pisang
            case 57: o = new Bibit(57,9,4); break; //mangga
            case 41: o = new BuahCabai(); break; //cabai
            case 42: o = new BuahWortel(); break; //wortel
            case 43: o = new BuahJamur(); break; //jamur
            case 44: o = new BuahMelon(); break; //melon
            case 45: o = new BuahJagung(); break; //jagung
            case 46: o = new BuahPisang(); break; //pisang
            case 47: o = new BuahMangga(); break; //mangga
        }
        if(nslot==1)
        {
            inventory1.clear();
            for(int i=0;i<quantity;i++) inventory1.push_back(o);
        }
        else if(nslot==2)
        {
            inventory2.clear();
            for(int i=0;i<quantity;i++) inventory2.push_back(o);
        }
        else if(nslot==3)
        {
            inventory3.clear();
            for(int i=0;i<quantity;i++) inventory3.push_back(o);
        }
    }

    //==========  other methods  ==========
    void player::displaypos()
    {
        cout<< "\n";
        cout<< "\t(" << location.up.x << "," << location.up.y << ")\n";
        cout<< "(" << location.left.x << "," << location.left.y << ")\t";
        cout<< "(" << location.mid.x << "," << location.mid.y << ")\t";
        cout<< "(" << location.right.x << "," << location.right.y << ")\n";
        cout<< "\t(" << location.down.x << "," << location.down.y << ")\n";
        cout<< "vector: (" << vektor.x << "," << vektor.y << ")\n";
    }
    void player::setFace (int dir)
    //set the direction player face
    {
        if      (dir == 0)   {vektor = location.left; face = 0;}
        else if (dir == 1)   {vektor = location.right; face = 1;}
        else if (dir == 2)   {vektor = location.up; face = 2;}
        else if (dir == 3)   {vektor = location.down; face = 3;}
    }
    void player::screenPos()
    {
        gotoxy(location.mid.x, location.mid.y);
        if (face == 0)       {cout<< "<\n";}
        else if (face == 1)  {cout<< ">\n";}
        else if (face == 2)  {cout<< "^\n";}
        else if (face == 3)  {cout<< "v\n";}
    }
    void player::moveLeft()
    {
        if (AP > 0)
        {
            if (face == 0)
            {
                point current = getpos();
                setpos(current.x - 1, current.y);
                setvektor(location.left);
                AP--;
            }
            else turn_left();
        }
        else sleep();
    }
    void player::moveRight()
    {
        if (AP > 0)
        {
            if (face == 1)
            {
                point current = getpos();
                setpos(current.x + 1, current.y);
                setvektor(location.right);
                AP--;
            }
            else turn_right();
        }
        else sleep();
    }
    void player::moveUp()
    {
        if (AP > 0)
        {
            if (face == 2)
            {
                point current = getpos();
                setpos(current.x, current.y - 1);
                setvektor(location.up);
                AP--;
            }
            else turn_up();
        }
        else sleep();
    }
    void player::moveDown()
    {
        if (AP > 0)
        {
            if (face == 3)
            {
                point current = getpos();
                setpos(current.x, current.y + 1);
                setvektor(location.down);
                AP--;
            }
            else turn_down();
        }
        else sleep();
    }
    void player::move()
    {
        //cout<< "press arrow key\n";
        int x = getch();
        if (x == 224)
        {
            x = getch();
            /*
            if (x == 75 || x == 72 && location.mid.x == 0 && location.mid.y == 0)
                setpos(0,0);
            else if (x == 75 || x == 72 && location.mid.x == 0)
            {
                point current = getpos();
                setpos(0,current.y);
            }
            else if (x == 75 || x == 72 && location.mid.y == 0)
            {
                point current = getpos();
                setpos(current.x, 0);
            }
            else
            {*/
                if      (x == 75) {moveLeft();}
                else if (x == 77) {moveRight();}
                else if (x == 72) {moveUp();}
                else if (x == 80) {moveDown();}
            //}
        }
    }
    void player::walk(int face, int steps)
    //Set walk step as much as <int>
    {
        // basis
        if      (face == 0 && steps == 1) {moveLeft();}
        else if (face == 1 && steps == 1) {moveRight();}
        else if (face == 2 && steps == 1) {moveUp();}
        else if (face == 3 && steps == 1) {moveDown();}
        // rekurens
        else if (face == 0 && steps > 1) {moveLeft(); walk(face, steps-1);}
        else if (face == 1 && steps > 1) {moveRight(); walk(face, steps-1);}
        else if (face == 2 && steps > 1) {moveUp(); walk(face, steps-1);}
        else if (face == 3 && steps > 1) {moveDown(); walk(face, steps-1);}
    }

    /******************************************************/
    /*      CORELATION WITH CLASS PLANT, BUAH, BIBIT      */
    /******************************************************/
    void player::slash(field * afield)
    //slash a true isMature/isHarvest/isDied plant
    //remove a plant from ground. Reset the field. 1 AP used
    //read class Plant and it's inheritance
    {
        int x = vektor.x;
        int y = vektor.y;
        if (afield->IsSlashable(x,y))
        {
            afield->DeleteObject(x,y);
            afield->InsertObject(new Tanah(false,false),x,y);
            //cout<< "slash success\n";
            AP--;
        }
        else
        {
            gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
            gotoxy(0,18); cout<< "Slash failed\n";
            gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
        }
    }
    void player::plow(field * afield)
    //plow a square of field to plant a seed.
    //set isPlow true. 1 AP used
    //read class Plant and it's inheritance
    {
        int x = vektor.x;
        int y = vektor.y;
        if (afield->IsPlowable(x,y))
        {
            Tanah * ground = dynamic_cast<Tanah*>(afield->getObject(x,y));
            ground->SetPlowed(true);
            //ground->isPlowed()? cout<< "Plow success\n": cout<< "not Plow\n";
            AP--;
        }
        else
        {
            gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
            gotoxy(0,18); cout<< "Plow failed\n";
            gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
        }
    }
    void player::water(field * afield)
    //water a square of field whether it contain Plant or no
    //set isWatered true. Level up 1 Happy Meter. 1 AP used
    //read class Plant and it's inheritance
    {
        int x = vektor.x;
        int y = vektor.y;
        //cout<< "start water\n";
        if (afield->IsWaterable(x,y))
        {
            Plant* p = dynamic_cast<Plant*>(afield->getObject(x,y));
            p->watered();
            p->isWatered()? cout<< "water success\n": cout<< "not watered\n";
            AP--;
        }
        else
        {
            gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
            gotoxy(0,18); cout<< "Watering failed\n";
            gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
        }
    }
    void player::harvest(field * afield)
    //harvest a square of field containing plant which isHarvest = true
    //change status to isDied = true, or revert to isMature = true. 1 AP used
    //read class Plant and it's inheritance
    //read class Buah, class Plant and it's inheritance
    {
        int x = vektor.x;
        int y = vektor.y;
        bool availability = false;
        Plant* p = dynamic_cast<Plant*>(afield->getObject(vektor.x,vektor.y));
        /*int seed = p->GetPlantId();
        switch (seed)
        {
            case 0: BuahCabai Buah; break;
            case 1: BuahMangga Buah; break;
            case 2: BuahWortel Buah; break;
            case 3: BuahJamur Buah; break;
            case 4: BuahMelon Buah; break;
            case 5: BuahPisang Buah; break;
            case 6: BuahJagung Buah; break;
        }*/
        //iterate and store buah ID
        int slot1 = 0;
        int slot2 = 0;
        int slot3 = 0;
        Fruit* buah1;
        Fruit* buah2;
        Fruit* buah3;
        if (!inventory1.empty() && inventory1.back()->getObjectType() == 4)
        {
            buah1 = dynamic_cast<Fruit*>(inventory1.back());
            slot1 = buah1->GetFruit_id();
        }
        if (!inventory2.empty() && inventory2.back()->getObjectType() == 4)
        {
            buah2 = dynamic_cast<Fruit*>(inventory2.back());
            slot2 = buah2->GetFruit_id();
        }
        if (!inventory3.empty() && inventory3.back()->getObjectType() == 4)
        {
            buah3 = dynamic_cast<Fruit*>(inventory3.back());
            slot3 = buah3->GetFruit_id();
        }
        //================================
        int ID = p->GetPlantId();
        if ((ID == 0 && slot1 == 41) || (ID == 0 && slot2 == 41) || (ID == 0 && slot3 == 41) ||
        (ID == 1 && slot1 == 47) || (ID == 1 && slot2 == 47) || (ID == 1 && slot3 == 47) ||
        (ID == 2 && slot1 == 42) || (ID == 2 && slot2 == 42) || (ID == 2 && slot3 == 42) ||
        (ID == 3 && slot1 == 43) || (ID == 3 && slot2 == 43) || (ID == 3 && slot3 == 43) ||
        (ID == 4 && slot1 == 44) || (ID == 4 && slot2 == 44) || (ID == 4 && slot3 == 44) ||
        (ID == 5 && slot1 == 46) || (ID == 5 && slot2 == 46) || (ID == 5 && slot3 == 46) ||
        (ID == 6 && slot1 == 45) || (ID == 6 && slot2 == 45) || (ID == 6 && slot3 == 45) ||
        inventory1.empty() || inventory2.empty() || inventory3.empty())
            {availability = true;}
		 
        if (afield->IsHarvestable(vektor.x,vektor.y) && availability)
        {
            p->harvested();
                 if (slot1 == 41) inventory1.push_back(new BuahCabai());
            else if (slot1 == 42) inventory1.push_back(new BuahWortel());
            else if (slot1 == 43) inventory1.push_back(new BuahJamur());
            else if (slot1 == 44) inventory1.push_back(new BuahMelon());
            else if (slot1 == 45) inventory1.push_back(new BuahJagung());
            else if (slot1 == 46) inventory1.push_back(new BuahPisang());
            else if (slot1 == 47) inventory1.push_back(new BuahMangga());
            else if (slot2 == 41) inventory2.push_back(new BuahCabai());
            else if (slot2 == 42) inventory2.push_back(new BuahWortel());
            else if (slot2 == 43) inventory2.push_back(new BuahJamur());
            else if (slot2 == 44) inventory2.push_back(new BuahMelon());
            else if (slot2 == 45) inventory2.push_back(new BuahJagung());
            else if (slot2 == 46) inventory2.push_back(new BuahPisang());
            else if (slot2 == 47) inventory2.push_back(new BuahMangga());
            else if (slot3 == 41) inventory3.push_back(new BuahCabai());
            else if (slot3 == 42) inventory3.push_back(new BuahWortel());
            else if (slot3 == 43) inventory3.push_back(new BuahJamur());
            else if (slot3 == 44) inventory3.push_back(new BuahMelon());
            else if (slot3 == 45) inventory3.push_back(new BuahJagung());
            else if (slot3 == 46) inventory3.push_back(new BuahPisang());
            else if (slot3 == 47) inventory3.push_back(new BuahMangga());
            else if (inventory1.empty()) 
            {
                switch (ID)
                {
                    case 0: inventory1.push_back(new BuahCabai()); break;
                    case 1: inventory1.push_back(new BuahMangga()); break;
                    case 2: inventory1.push_back(new BuahWortel()); break;
                    case 3: inventory1.push_back(new BuahJamur()); break;
                    case 4: inventory1.push_back(new BuahMelon()); break;
                    case 5: inventory1.push_back(new BuahPisang()); break;
                    case 6: inventory1.push_back(new BuahJagung()); break;
                }
            }
            else if (inventory2.empty()) 
            {
                switch (ID)
                {
                    case 0: inventory2.push_back(new BuahCabai()); break;
                    case 1: inventory2.push_back(new BuahMangga()); break;
                    case 2: inventory2.push_back(new BuahWortel()); break;
                    case 3: inventory2.push_back(new BuahJamur()); break;
                    case 4: inventory2.push_back(new BuahMelon()); break;
                    case 5: inventory2.push_back(new BuahPisang()); break;
                    case 6: inventory2.push_back(new BuahJagung()); break;
                }
            }
            else if (inventory3.empty())
            {
                switch (ID)
                {
                    case 0: inventory3.push_back(new BuahCabai()); break;
                    case 1: inventory3.push_back(new BuahMangga()); break;
                    case 2: inventory3.push_back(new BuahWortel()); break;
                    case 3: inventory3.push_back(new BuahJamur()); break;
                    case 4: inventory3.push_back(new BuahMelon()); break;
                    case 5: inventory3.push_back(new BuahPisang()); break;
                    case 6: inventory3.push_back(new BuahJagung()); break;
                }
            }
            //p->isHarvest()? cout<< "not yet harvest\n": cout<< "successfully harvested\n";
            AP--;
            gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
        }
        else 
        {
            gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
            gotoxy(0,18); cout<< "Harvest failed\n";
            gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
        }
    }
    void player::eat(int inv_num)
    //eat an inventory item which isFood true. Nothing happened when isFood false
    //read class Buah, class Plant and it's inheritance
    {
        int newAP = getAP();
        Fruit *afruit;
        if (inv_num >= 1 && inv_num <= 3)
        {
            switch (inv_num)
            {
                case 1:
                    //delete inventory vector pointer it
                    if (!inventory1.empty() && inventory1.back()->getObjectType() == 4)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory1.back());
                        newAP = newAP + afruit->GetAP();
                        setAP(newAP);
                        inventory1.pop_back();
                    }
                    else
                    {
                        gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                        gotoxy(0,18); cout<< "You can't eat this\n";
                        gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                    }
                    break;
                case 2:
                    //delete inventory vector pointer it
                    if (!inventory2.empty() && inventory2.back()->getObjectType() == 4)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory2.back());
                        newAP = newAP + afruit->GetAP();
                        setAP(newAP);
                        inventory2.pop_back();
                    }
                    else 
                    {
                        gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                        gotoxy(0,18); cout<< "You can't eat this\n";
                        gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                    }
                    break;
                case 3:
                    //delete inventory vector pointer it
                    if (!inventory3.empty() && inventory3.back()->getObjectType() == 4)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory3.back());
                        newAP = newAP + afruit->GetAP();
                        setAP(newAP);
                        inventory3.pop_back();
                    }
                    else 
                    {
                        gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                        gotoxy(0,18); cout<< "You can't eat this\n";
                        gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                    }
                    break;
            }
        }
        else 
        {
            gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
            gotoxy(0,18); cout<< "not a valid slot to eat\n";
            gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
        }
        gotoxy(0,17); cout<< "\t\t\t\t\t\t\t\t\t";
        gotoxy(0,17);
        //AP--;
    }
    void player::put(int slot, int quantity, field* afield)
    //discard or sow the seed. put or discard the plant
    //read class Buah, class Plant and it's inheritance
    {   //Plant the seed
        if (afield->IsPlantable(vektor.x, vektor.y) == true)
        {
            if (slot == 1)
            {
                if (inventory1.empty())
                {
                    gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                    gotoxy(0,18); cout<< "Slot is empty, you can do nothing with it\n";
                    gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                }
                else if (inventory1.back()->getObjectType() == 5 && quantity == 1)
                {
                    Bibit* seed = dynamic_cast<Bibit*>(inventory1.back());
                    switch (seed->getBibitId())
                    {
                        case 51:
                            afield->InsertPlant(new Cabai(),vektor.x,vektor.y);
                            inventory1.pop_back();
                            break;
                        case 52:
                            afield->InsertPlant(new Wortel(),vektor.x,vektor.y);
                            inventory1.pop_back();
                            break;
                        case 53:
                            afield->InsertPlant(new Jamur(),vektor.x,vektor.y);
                            inventory1.pop_back();
                            break;
                        case 54:
                            afield->InsertPlant(new Melon(),vektor.x,vektor.y);
                            inventory1.pop_back();
                            break;
                        case 55:
                            afield->InsertPlant(new Jagung(),vektor.x,vektor.y);
                            inventory1.pop_back();
                            break;
                        case 56:
                            afield->InsertPlant(new Pisang(),vektor.x,vektor.y);
                            inventory1.pop_back();
                            break;
                        case 57:
                            afield->InsertPlant(new Mangga(),vektor.x,vektor.y);
                            inventory1.pop_back();
                            break;
                    } //end switch
                }
                else
                {
                    gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                    gotoxy(0,18); cout<< "Discard item from slot 1\n";
                    gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                    for (int i = 0; i<= quantity; i++)
                    {
                        while (!inventory1.empty())
                            {inventory1.pop_back();}
                    }
                }
            }
            else if (slot == 2)
            {
                if (inventory2.empty())
                {
                    gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                    gotoxy(0,18); cout<< "Slot is empty, you can do nothing with it\n";
                    gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                }
                else if (inventory2.back()->getObjectType() == 5 && quantity == 1)
                {
                    Bibit* seed = dynamic_cast<Bibit*>(inventory2.back());
                    switch (seed->getBibitId())
                    {
                        case 51:
                            afield->InsertPlant(new Cabai(),vektor.x,vektor.y);
                            inventory2.pop_back();
                            break;
                        case 52:
                            afield->InsertPlant(new Wortel(),vektor.x,vektor.y);
                            inventory2.pop_back();
                            break;
                        case 53:
                            afield->InsertPlant(new Jamur(),vektor.x,vektor.y);
                            inventory2.pop_back();
                            break;
                        case 54:
                            afield->InsertPlant(new Melon(),vektor.x,vektor.y);
                            inventory2.pop_back();
                            break;
                        case 55:
                            afield->InsertPlant(new Jagung(),vektor.x,vektor.y);
                            inventory2.pop_back();
                            break;
                        case 56:
                            afield->InsertPlant(new Pisang(),vektor.x,vektor.y);
                            inventory2.pop_back();
                            break;
                        case 57:
                            afield->InsertPlant(new Mangga(),vektor.x,vektor.y);
                            inventory2.pop_back();
                            break;
                    } //end switch
                }
                else
                {
                    gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                    gotoxy(0,18); cout<< "Discard item from slot 2\n";
                    gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                    for (int i = 0; i<= quantity; i++)
                    {
                        while (!inventory2.empty())
                            {inventory2.pop_back();}
                    }
                }
            }
            else if (slot == 3)
            {
                if (inventory3.empty())
                {
                    gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                    gotoxy(0,18); cout<< "Slot is empty, you can do nothing with it\n";
                    gotoxy(0,17); cout<< ">\t\t\t\t\t\t\t\t";
                }
                else if (inventory3.back()->getObjectType() == 5 && quantity == 1)
                {
                    Bibit* seed = dynamic_cast<Bibit*>(inventory3.back());
                    switch (seed->getBibitId())
                    {
                        case 51:
                            afield->InsertPlant(new Cabai(),vektor.x,vektor.y);
                            inventory3.pop_back();
                            break;
                        case 52:
                            afield->InsertPlant(new Wortel(),vektor.x,vektor.y);
                            inventory3.pop_back();
                            break;
                        case 53:
                            afield->InsertPlant(new Jamur(),vektor.x,vektor.y);
                            inventory3.pop_back();
                            break;
                        case 54:
                            afield->InsertPlant(new Melon(),vektor.x,vektor.y);
                            inventory3.pop_back();
                            break;
                        case 55:
                            afield->InsertPlant(new Jagung(),vektor.x,vektor.y);
                            inventory3.pop_back();
                            break;
                        case 56:
                            afield->InsertPlant(new Pisang(),vektor.x,vektor.y);
                            inventory3.pop_back();
                            break;
                        case 57:
                            afield->InsertPlant(new Mangga(),vektor.x,vektor.y);
                            inventory3.pop_back();
                            break;
                    } //end switch
                }
                else
                {
                    gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                    gotoxy(0,18); cout<< "Discard item from slot 1\n";
                    gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                    for (int i = 0; i<= quantity; i++)
                    {
                        while (!inventory3.empty())
                            {inventory3.pop_back();}
                    }
                }
            }
        }
        else if (afield->IsSellingSpot(vektor.x, vektor.y)) //If player face the selling spot
        {
            int i = 0;
            int current_money;
            Fruit * afruit;
            Bibit * seed;
            switch (slot)
            {
                case 1:
                    if (inventory1.back()->getObjectType() == 5)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory1.back());
                        while (i <= quantity && !inventory1.empty())
                        {
                            current_money = getMoney() + afruit->GetHarga();
                            setMoney(current_money);
                            inventory1.pop_back();
                            i++;
                        }
                    }
                    else if (inventory1.back()->getObjectType() == 4)
                    {
                        seed = dynamic_cast<Bibit*>(inventory1.back());
                        while (i <= quantity && !inventory1.empty())
                        {
                            current_money = getMoney() + seed->getHargaJual();
                            setMoney(current_money);
                            inventory1.pop_back();
                            i++;
                        }
                    }
                    break;
                case 2:
                    if (inventory2.back()->getObjectType() == 5)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory2.back());
                        while (i <= quantity && !inventory2.empty())
                        {
                            current_money = getMoney() + afruit->GetHarga();
                            setMoney(current_money);
                            inventory2.pop_back();
                            i++;
                        }
                    }
                    else if (inventory2.back()->getObjectType() == 4)
                    {
                        seed = dynamic_cast<Bibit*>(inventory2.back());
                        while (i <= quantity && !inventory2.empty())
                        {
                            current_money = getMoney() + seed->getHargaJual();
                            setMoney(current_money);
                            inventory2.pop_back();
                            i++;
                        }
                    }
                    break;
                case 3:
                    if (inventory3.back()->getObjectType() == 5)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory3.back());
                        while (i <= quantity && !inventory3.empty())
                        {
                            current_money = getMoney() + afruit->GetHarga();
                            setMoney(current_money);
                            inventory3.pop_back();
                            i++;
                        }
                    }
                    else if (inventory3.back()->getObjectType() == 4)
                    {
                        seed = dynamic_cast<Bibit*>(inventory3.back());
                        while (i <= quantity && !inventory3.empty())
                        {
                            current_money = getMoney() + seed->getHargaJual();
                            setMoney(current_money);
                            inventory3.pop_back();
                            i++;
                        }
                    }
                    break;
            }
        }
        else //if player not facing a plough field nor selling spot nor quantity > 1
        {
            int i = 0;
            switch (slot)
            {
                case 1:
                    while (i <= quantity && !inventory1.empty())
                    {
                        inventory1.pop_back();
                        i++;
                    }
                    break;
                case 2:
                    while (i <= quantity && !inventory2.empty())
                    {
                        inventory2.pop_back();
                        i++;
                    }
                    break;
                case 3:
                    while (i <= quantity && !inventory3.empty())
                    {
                        inventory3.pop_back();
                        i++;
                    }
                    break;
            }
        }
        gotoxy(40,2); cout<< "\t\t          ";
        gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
        gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
        AP--;
    }
    void player::buy(int item, int quantity)
    //buy item from store
    //read class Bibit
    {
        /* Please display item in store in this order:
         * 51 - Bibit Cabai
         * 52 - Bibit Wortel
         * 53 - Bibit Jamur
         * 54 - Bibit Melon
         * 55 - Bibit Jagung
         * 56 - Bibit Pisang
         * 57 - Bibit Mangga
         */
        if (getArea() == 2)
        {
            int seed_ID;
            int counter;
            int current_money, price;
            Bibit * seed;
            int slot1 = 0;
            int slot2 = 0;
            int slot3 = 0;
            Bibit* seed1;
            Bibit* seed2;
            Bibit* seed3;
            if (!inventory1.empty() && inventory1.back()->getObjectType() == 5)
            {
                seed1 = dynamic_cast<Bibit*>(inventory1.back());
                slot1 = seed1->getBibitId();
            }
            if (!inventory2.empty() && inventory2.back()->getObjectType() == 5)
            {
                seed2 = dynamic_cast<Bibit*>(inventory2.back());
                slot2 = seed2->getBibitId();
            }
            if (!inventory3.empty() && inventory3.back()->getObjectType() == 5)
            {
                seed3 = dynamic_cast<Bibit*>(inventory3.back());
                slot3 = seed3->getBibitId();
            }
            switch (item)
            {
                case 1: seed_ID = 51; price = 10; break;
                case 2: seed_ID = 52; price = 5; break;
                case 3: seed_ID = 53; price = 8; break;
                case 4: seed_ID = 54; price = 6; break;
                case 5: seed_ID = 55; price = 7; break;
                case 6: seed_ID = 56; price = 6; break;
                case 7: seed_ID = 57; price = 9; break;
            }
            if (getMoney() >= price*quantity)
            {
                if (!inventory1.empty() && slot1 == seed_ID)
                {
                    seed = dynamic_cast<Bibit*>(inventory1.back());
                    current_money = money - price * quantity;
                    setMoney(current_money);
                    gotoxy(40,2); cout<< "\t\t          ";
                    gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
                    for (counter = 1; counter <= quantity; counter++)
                    {
                        switch(seed_ID)
                        {
                            case 51: inventory1.push_back(new Bibit(51,10,5)); break; //cabai
                            case 52: inventory1.push_back(new Bibit(52,5,2)); break; //wortel
                            case 53: inventory1.push_back(new Bibit(53,8,4)); break; //jamur
                            case 54: inventory1.push_back(new Bibit(54,6,3)); break; //melon
                            case 55: inventory1.push_back(new Bibit(55,7,3)); break; //jagung
                            case 56: inventory1.push_back(new Bibit(56,6,3)); break; //pisang
                            case 57: inventory1.push_back(new Bibit(57,9,4)); break; //mangga
                        }
                    }
                }
                else if (!inventory2.empty() && slot2 == seed_ID)
                {
                    seed = dynamic_cast<Bibit*>(inventory2.back());
                    current_money = money - price * quantity;
                    setMoney(current_money);
                    gotoxy(40,2); cout<< "\t\t          ";
                    gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
                    for (counter = 1; counter <= quantity; counter++)
                    {
                        switch(seed_ID)
                        {
                            case 51: inventory2.push_back(new Bibit(51,10,5)); break; //cabai
                            case 52: inventory2.push_back(new Bibit(52,5,2)); break; //wortel
                            case 53: inventory2.push_back(new Bibit(53,8,4)); break; //jamur
                            case 54: inventory2.push_back(new Bibit(54,6,3)); break; //melon
                            case 55: inventory2.push_back(new Bibit(55,7,3)); break; //jagung
                            case 56: inventory2.push_back(new Bibit(56,6,3)); break; //pisang
                            case 57: inventory2.push_back(new Bibit(57,9,4)); break; //mangga
                        }
                    }
                }
                else if (!inventory3.empty() && slot3 == seed_ID)
                {
                    seed = dynamic_cast<Bibit*>(inventory3.back());
                    current_money = money - price * quantity;
                    setMoney(current_money);
                    gotoxy(40,2); cout<< "\t\t          ";
                    gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
                    for (counter = 1; counter <= quantity; counter++)
                    {
                        switch(seed_ID)
                        {
                            case 51: inventory3.push_back(new Bibit(51,10,5)); break; //cabai
                            case 52: inventory3.push_back(new Bibit(52,5,2)); break; //wortel
                            case 53: inventory3.push_back(new Bibit(53,8,4)); break; //jamur
                            case 54: inventory3.push_back(new Bibit(54,6,3)); break; //melon
                            case 55: inventory3.push_back(new Bibit(55,7,3)); break; //jagung
                            case 56: inventory3.push_back(new Bibit(56,6,3)); break; //pisang
                            case 57: inventory3.push_back(new Bibit(57,9,4)); break; //mangga
                        }
                    }
                }
                else if (!inventory1.empty() && !inventory2.empty() && !inventory3.empty())
                {
                    gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                    gotoxy(0,18); cout<< "Your inventory is full\n";
                    gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
                }
                else if (inventory1.empty())
                {
                    current_money = money - price * quantity;
                    setMoney(current_money);
                    gotoxy(40,2); cout<< "\t\t          ";
                    gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
                    for (counter = 1; counter <= quantity; counter++)
                    {
                        switch(seed_ID)
                        {
                            case 51: inventory1.push_back(new Bibit(51,10,5)); break; //cabai
                            case 52: inventory1.push_back(new Bibit(52,5,2)); break; //wortel
                            case 53: inventory1.push_back(new Bibit(53,8,4)); break; //jamur
                            case 54: inventory1.push_back(new Bibit(54,6,3)); break; //melon
                            case 55: inventory1.push_back(new Bibit(55,7,3)); break; //jagung
                            case 56: inventory1.push_back(new Bibit(56,6,3)); break; //pisang
                            case 57: inventory1.push_back(new Bibit(57,9,4)); break; //mangga
                        }
                    }
                }
                else if (inventory2.empty())
                {
                    current_money = money - price * quantity;
                    setMoney(current_money);
                    gotoxy(40,2); cout<< "\t\t          ";
                    gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
                    for (counter = 1; counter <= quantity; counter++)
                    {
                        switch(seed_ID)
                        {
                            case 51: inventory2.push_back(new Bibit(51,10,5)); break; //cabai
                            case 52: inventory2.push_back(new Bibit(52,5,2)); break; //wortel
                            case 53: inventory2.push_back(new Bibit(53,8,4)); break; //jamur
                            case 54: inventory2.push_back(new Bibit(54,6,3)); break; //melon
                            case 55: inventory2.push_back(new Bibit(55,7,3)); break; //jagung
                            case 56: inventory2.push_back(new Bibit(56,6,3)); break; //pisang
                            case 57: inventory2.push_back(new Bibit(57,9,4)); break; //mangga
                        }
                    }
                }
                else if (inventory3.empty())
                {
                    current_money = money - price * quantity;
                    setMoney(current_money);
                    gotoxy(40,2); cout<< "\t\t          ";
                    gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
                    for (counter = 1; counter <= quantity; counter++)
                    {
                        switch(seed_ID)
                        {
                            case 51: inventory3.push_back(new Bibit(51,10,5)); break; //cabai
                            case 52: inventory3.push_back(new Bibit(52,5,2)); break; //wortel
                            case 53: inventory3.push_back(new Bibit(53,8,4)); break; //jamur
                            case 54: inventory3.push_back(new Bibit(54,6,3)); break; //melon
                            case 55: inventory3.push_back(new Bibit(55,7,3)); break; //jagung
                            case 56: inventory3.push_back(new Bibit(56,6,3)); break; //pisang
                            case 57: inventory3.push_back(new Bibit(57,9,4)); break; //mangga
                        }
                    }
                }
                gotoxy(0,17); cout<< "\t\t\t\t\t\t\t\t\t";
                gotoxy(0,17); cout<< ">";
            }
            else cout<< "Not enough money\n your money is: " << getMoney()<< endl;
        }
        else cout<< "You're not in store\n";
    }
    void player::sell(int slot, int quantity)
    //sell item from inventory
    //read class Bibit, class Buah
    //IMPORTANT!!!!!! Selling spot has not detected yet, only selling mechanism!!!!!!!
    {
        if (getArea() == 2)
        {
            int i = 1;
            int current_money;
            Fruit * afruit;
            Bibit * seed;
            switch (slot)
            {
                case 1:
                    if (!inventory1.empty() && inventory1.back()->getObjectType() == 5)
                    {
                        seed = dynamic_cast<Bibit*>(inventory1.back());
                        while (i <= quantity && !inventory1.empty())
                        {
                            current_money = getMoney() + seed->getHargaJual();
                            setMoney(current_money);
                            inventory1.pop_back();
                            i++;
                        }
                    }
                    else if (!inventory1.empty() && inventory1.back()->getObjectType() == 4)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory1.back());
                        while (i <= quantity && !inventory1.empty())
                        {
                            current_money = getMoney() + afruit->GetHarga();
                            setMoney(current_money);
                            inventory1.pop_back();
                            i++;
                        }
                    }
                    break;
                case 2:
                    if (!inventory2.empty() && inventory2.back()->getObjectType() == 5)
                    {
                        seed = dynamic_cast<Bibit*>(inventory2.back());
                        while (i <= quantity && !inventory2.empty())
                        {
                            current_money = getMoney() + seed->getHargaJual();
                            setMoney(current_money);
                            inventory2.pop_back();
                            i++;
                        }
                    }
                    else if (!inventory2.empty() && inventory2.back()->getObjectType() == 4)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory2.back());
                        while (i <= quantity && !inventory2.empty())
                        {
                            current_money = getMoney() + afruit->GetHarga();
                            setMoney(current_money);
                            inventory2.pop_back();
                            i++;
                        }
                    }
                    break;
                case 3:
                    if (!inventory3.empty() && inventory3.back()->getObjectType() == 5)
                    {
                        seed = dynamic_cast<Bibit*>(inventory3.back());
                        while (i <= quantity && !inventory3.empty())
                        {
                            current_money = getMoney() + seed->getHargaJual();
                            setMoney(current_money);
                            inventory3.pop_back();
                            i++;
                        }
                    }
                    else if (!inventory3.empty() && inventory3.back()->getObjectType() == 4)
                    {
                        afruit = dynamic_cast<Fruit*>(inventory3.back());
                        while (i <= quantity && !inventory3.empty())
                        {
                            current_money = getMoney() + afruit->GetHarga();
                            setMoney(current_money);
                            inventory3.pop_back();
                            i++;
                        }
                    }
                    break;
            }
            gotoxy(40,2); cout<< "\t\t          ";
            gotoxy(40,2); cout<< "Money\t: " << getMoney() << endl;
        } else
        {
            gotoxy(0,18); cout<< "\t\t\t\t\t\t\t\t\t\t\t\t\t";
            gotoxy(0,18); cout<< "You're not in the store so you can't sell here";
        }
    }
    void player::displayInventory()
    //show all item in player inventory and the quantity
    //access player property inventory
    {
        // Inventory 1
        //=====================================================================
        if (inventory1.empty())
        {
            gotoxy(40,9); cout<< "\t\t\t\t\t";
            gotoxy(40,9); cout<< "Slot 1 Empty";
        }
        else if (inventory1.front()->getObjectType() == 4)
        {
			
             Fruit *afruit = dynamic_cast <Fruit*> (inventory1.front());
             int type = afruit->GetFruit_id();
             gotoxy(40,9); cout<< "\t\t\t\t\t";
             gotoxy(40,9);
             switch (type)
             {
                 case 41: cout<< "Cabai\t\t- " << inventory1.size(); break;
                 case 42: cout<< "Wortel\t\t- " << inventory1.size(); break;
                 case 43: cout<< "Jamur\t\t- " << inventory1.size(); break;
                 case 44: cout<< "Melon\t\t- " << inventory1.size(); break;
                 case 45: cout<< "Jagung\t\t- " << inventory1.size(); break;
                 case 46: cout<< "Pisang\t\t- " << inventory1.size(); break;
                 case 47: cout<< "Mangga\t\t- " << inventory1.size(); break;
             }
        }
        else if (inventory1.front()->getObjectType() == 5)
        {
             Bibit *abibit = dynamic_cast <Bibit*> (inventory1.front());
             int type = abibit->getBibitId();
             gotoxy(40,9); cout<< "\t\t\t\t\t";
             gotoxy(40,9);
             switch (type)
             {
                 case 51: cout<< "Bibit Cabai\t- " << inventory1.size(); break;
                 case 52: cout<< "Bibit Wortel\t- " << inventory1.size(); break;
                 case 53: cout<< "Bibit Jamur\t- " << inventory1.size(); break;
                 case 54: cout<< "Bibit Melon\t- " << inventory1.size(); break;
                 case 55: cout<< "Bibit Jagung\t- " << inventory1.size(); break;
                 case 56: cout<< "Bibit Pisang\t- " << inventory1.size(); break;
                 case 57: cout<< "Bibit Mangga\t- " << inventory1.size(); break;
             }
         }

         // Inventory 2
         //=====================================================================
        if (inventory2.empty())
        {
            gotoxy(40,10); cout<< "\t\t\t\t\t";
            gotoxy(40,10); cout<< "Slot 2 Empty";
        }
        else if (inventory2.front()->getObjectType() == 4)
        {
             Fruit *afruit = dynamic_cast <Fruit*> (inventory2.front());
             int type = afruit->GetFruit_id();
             gotoxy(40,10); cout<< "\t\t\t\t\t";
             gotoxy(40,10);
             switch (type)
             {
                 case 41: cout<< "Cabai\t\t- " << inventory2.size(); break;
                 case 42: cout<< "Wortel\t\t- " << inventory2.size(); break;
                 case 43: cout<< "Jamur\t\t- " << inventory2.size(); break;
                 case 44: cout<< "Melon\t\t- " << inventory2.size(); break;
                 case 45: cout<< "Jagung\t\t- " << inventory2.size(); break;
                 case 46: cout<< "Pisang\t\t- " << inventory2.size(); break;
                 case 47: cout<< "Mangga\t\t- " << inventory2.size(); break;
             }
        }
        else if (inventory2.front()->getObjectType() == 5)
        {
             Bibit *abibit = dynamic_cast <Bibit*> (inventory2.front());
             int type = abibit->getBibitId();
             gotoxy(40,10); cout<< "\t\t\t\t\t";
             gotoxy(40,10);
             switch (type)
             {
                 case 51: cout<< "Bibit Cabai\t- " << inventory2.size(); break;
                 case 52: cout<< "Bibit Wortel\t- " << inventory2.size(); break;
                 case 53: cout<< "Bibit Jamur\t- " << inventory2.size(); break;
                 case 54: cout<< "Bibit Melon\t- " << inventory2.size(); break;
                 case 55: cout<< "Bibit Jagung\t- " << inventory2.size(); break;
                 case 56: cout<< "Bibit Pisang\t- " << inventory2.size(); break;
                 case 57: cout<< "Bibit Mangga\t- " << inventory2.size(); break;
             }
         }

         // Inventory 3
         //=====================================================================
        if (inventory3.empty())
        {
            gotoxy(40,11); cout<< "\t\t\t\t\t";
            gotoxy(40,11); cout<< "Slot 3 Empty";
        }
        else if (inventory3.front()->getObjectType() == 4)
        {
             Fruit *afruit = dynamic_cast <Fruit*> (inventory3.front());
             int type = afruit->GetFruit_id();
             gotoxy(40,11); cout<< "\t\t\t\t\t";
             gotoxy(40,11);
             switch (type)
             {
                 case 41: cout<< "Cabai\t\t- " << inventory3.size(); break;
                 case 42: cout<< "Wortel\t\t- " << inventory3.size(); break;
                 case 43: cout<< "Jamur\t\t- " << inventory3.size(); break;
                 case 44: cout<< "Melon\t\t- " << inventory3.size(); break;
                 case 45: cout<< "Jagung\t\t- " << inventory3.size(); break;
                 case 46: cout<< "Pisang\t\t- " << inventory3.size(); break;
                 case 47: cout<< "Mangga\t\t- " << inventory3.size(); break;
             }
        }
        else if (inventory3.front()->getObjectType() == 5)
        {
             Bibit *abibit = dynamic_cast <Bibit*> (inventory3.front());
             int type = abibit->getBibitId();
             gotoxy(40,11); cout<< "\t\t\t\t\t";
             gotoxy(40,11);
             switch (type)
             {
                 case 51: cout<< "Bibit Cabai\t- " << inventory3.size(); break;
                 case 52: cout<< "Bibit Wortel\t- " << inventory3.size(); break;
                 case 53: cout<< "Bibit Jamur\t- " << inventory3.size(); break;
                 case 54: cout<< "Bibit Melon\t- " << inventory3.size(); break;
                 case 55: cout<< "Bibit Jagung\t- " << inventory3.size(); break;
                 case 56: cout<< "Bibit Pisang\t- " << inventory3.size(); break;
                 case 57: cout<< "Bibit Mangga\t- " << inventory3.size(); break;
             }
        }
        gotoxy(0,17); cout<< ">\t\t\t\t\t"; gotoxy(0,17); cout<< ">";
    }

    /****************************************/
    /*      CORELATION WITH CLASS MAP       */
    /****************************************/
    void player::sleep()
    //go to sleep, change the day, set all field to isWatered false
    //change to nextday, set all iswatered to false
    //access class Field, class Plant
    {
        AP = 0;
    }
	
